package Txtfiction;

import Txtfiction.common.Request;
import Txtfiction.common.Response;

import java.io.*;
import java.net.Socket;
import java.util.List;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        Socket s= null;
        try {
            s = new Socket("localhost",8888);
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            ObjectInputStream ois=new ObjectInputStream(in);
            ObjectOutputStream oos = new ObjectOutputStream(out);
            Scanner input=new Scanner(System.in);
            Request r=new Request();
            while (true){
                System.out.println("欢迎使用在线迷你TXT小说管理器");
                System.out.println("1.登录"+"\t"+"2.注册"+"\t"+"3.退出");
                System.out.print("请选择:");
                int choice=input.nextInt();
                if (choice==1) {
                    for (;;) {
                        r.setAction("login");
                        System.out.println("用户名");
                        r.put("name", input.next());
                        System.out.println("密码:");
                        r.put("pass", input.next());
                        //发送r
                        oos.writeObject(r);
                        oos.reset();
                        oos.flush();
                        Response response = (Response) ois.readObject();
                        System.out.println(response.getMsg());
                        if (response.isSuccess() == false) {
                            System.out.println("用户名和密码错误，请重新输入");
                        }else {
                            break;
                        }
                    }
                        while(true) {
                        System.out.println("0.返回上级菜单" + "\t" + "1.武侠" + "\t" + "2.言情");
                        System.out.println("请选择:");
                        int choice1 = input.nextInt();
                        if (choice1==1||choice1==2){
                            System.out.println("序号"+"\t\t"+"名称"+"\t\t"+"作者"+"\t\t"+"简介");
                            r.setAction("readinfo");
                            r.put("choice", choice1);
                            oos.writeObject(r);
                            oos.reset();
                            oos.flush();
                            Response response6= (Response) ois.readObject();
                            List<FictionList> o = (List<FictionList>) response6.get("novels");
                            for (FictionList l:o) {
                                System.out.println(l);
                            }
                            System.out.println("小说列表结束-------------------");
                        }else if (choice1==0){
                            break;
                        }
                        while (true) {
                            System.out.print("阅读和下载请选择文件序号,上传TXT请输入-1,返回请输入0");
                            int choice2 = input.nextInt();
                            if (choice1 == 1) {
                                r.setAction("redwuxia");
                                r.put("choice1", choice2);
                                oos.writeObject(r);
                                oos.reset();
                                oos.flush();
                                Response response2 = (Response) ois.readObject();
                                String o = (String)response2.get("neirong");
                                System.out.println(o);
                                System.out.println(response2.getMsg());
                            } else if (choice1 == 2) {
                                r.setAction("redyanqing");
                                r.put("choice1", choice2);
                                oos.writeObject(r);
                                oos.reset();
                                oos.flush();
                                Response response2 = (Response) ois.readObject();
                                String o = (String)response2.get("neirong");
                                System.out.println(o);
                                System.out.println(response2.getMsg());
                            } else if (choice2 == -1) {
                                r.setAction("uploading");
                                System.out.print("请输入类型(1.武侠 2.言情):");
                                r.put("type", input.nextInt());
                                System.out.println("请输入小说名:");
                                r.put("fname", input.next());
                                System.out.print("请输入作者:");
                                r.put("author", input.next());
                                System.out.print("请输入简介:");
                                r.put("introduce", input.next());
                                System.out.print("请输入上传的txt(请注意路径用斜杠):");
                                r.put("location", input.next());
                                System.out.println("请输入内容:");
                                r.put("neirong",input.next());
                                //发送r
                                oos.writeObject(r);
                                oos.reset();
                                oos.flush();
                                Response response3 = (Response) ois.readObject();
                                System.out.println(response3.getMsg());
                                break;
                            } else if (choice2 == 0) {
                                break;
                            }
                        }
                        while (true) {
                            System.out.println("继续显示列表请输入1,下载TXT请输入2:");
                            int i = input.nextInt();
                            if (i == 1) {
                                break;
                            } else if (i == 2) {
                                r.setAction("download");
                                System.out.print("下载后的文件名是:");
                                r.put("filename", input.next());
                                //发送r
                                oos.writeObject(r);
                                oos.reset();
                                oos.flush();
                                System.out.print("下载后的路径是:");
                                String lujing="C:\\Users\\Administrator\\Desktop\\xinjian\\story"+input.next();
                                Response response2 = (Response) ois.readObject();
                                byte[] b =(byte[])response2.get("content");
                                FileOutputStream fos=new FileOutputStream(new File(lujing));
                                fos.write(b);
                                fos.close();
                                if (response2.isSuccess()==false){
                                    System.out.println("下载失败");
                                }else{
                                    System.out.println("下载成功");
                                }
                                System.out.println(response2.getMsg());
                                continue;
                            }
                        }
                    }
                }else if (choice==2){
                    r.setAction("register");
                    System.out.println("用户名:");
                    r.put("name",input.next());
                    System.out.println("密码:");
                    r.put("pass",input.next());
                    System.out.println("再次输入密码");
                    r.put("repeatpass",input.next());
                    oos.writeObject(r);;
                    oos.reset();
                    oos.flush();
                    Response response=(Response)ois.readObject();
                    System.out.println(response.getMsg());
                }else if (choice==3){
                    System.out.println("程序已退出");
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                s.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
